﻿using System.Text;
using System.Web.Mvc;
using Dexter.Core;
using Dexter.Dependency;
using Dexter.Services;

namespace Dexter.Web.Mvc.Helpers
{
	public static class StandardHelpers
	{
		public static MvcHtmlString Index(this HtmlHelper helper)
		{
			var u = DexterContainer.Resolve<IUrlBuilder>();
			var model = (Dexter.Web.Mvc.ViewModel.ViewsData.PublicBaseViewData)helper.ViewData.Model;			
			return new MvcHtmlString(string.Format("<link rel=\"index\" title=\"{0}\" href=\"{1}\" />",model.Title,u.HomePage));
		}

		public static MvcHtmlString Favicon(this HtmlHelper helper)
		{
			var uh = new UrlHelper(helper.ViewContext.RequestContext);

			string faviconUrl = uh.Content("~/Images/favicon.ico");

			return new MvcHtmlString(string.Format("<link href=\"{0}\" rel=\"shortcut icon\" type=\"image/x-icon\" />", faviconUrl));
		}

		public static MvcHtmlString WindowsLiveWriterManifest(this HtmlHelper helper)
		{
			var uh = new UrlHelper(helper.ViewContext.RequestContext);

			string wlwmanifestUrl = uh.Content("~/wlwmanifest.xml");

			return new MvcHtmlString(string.Format("<link href=\"{0}\" rel=\"wlwmanifest\" type=\"application/wlwmanifest+xml\" />", wlwmanifestUrl));
		}

		public static MvcHtmlString MetaWeblogRsd(this HtmlHelper helper)
		{
			var u = DexterContainer.Resolve<IUrlBuilder>();
			return new MvcHtmlString(string.Format("<link href=\"{0}\" rel=\"EditURI\" type=\"application/rsd+xml\" title=\"RSD\" />", u.MetaWeblogRsd));
		}

		public static MvcHtmlString OpenSearch(this HtmlHelper helper)
		{
			var u = DexterContainer.Resolve<IUrlBuilder>();

			var model = (Dexter.Web.Mvc.ViewModel.ViewsData.PublicBaseViewData)helper.ViewData.Model;
			return new MvcHtmlString(string.Format("<link href=\"{0}\" rel=\"search\" type=\"application/opensearchdescription+xml\" title=\"{1}\" />", u.OpenSearch, model.BlogTitle));
		}

		public static MvcHtmlString FeedUrl(this HtmlHelper helper)
		{
			var u = DexterContainer.Resolve<IUrlBuilder>();
			var model = (Dexter.Web.Mvc.ViewModel.ViewsData.PublicBaseViewData)helper.ViewData.Model;

			return new MvcHtmlString(string.Format("<link href=\"{0}\" rel=\"alternate\" type=\"application/rss+xml\" title=\"{1}\" />", u.FeedUrl, model.BlogTitle));
		}

		public static MvcHtmlString PingBack(this HtmlHelper helper)
		{
			var u = DexterContainer.Resolve<IUrlBuilder>();

			return new MvcHtmlString(string.Format("<link href=\"{0}\" rel=\"pingback\" />", u.PingBackUrl));
		}

		public static MvcHtmlString MetaDescription(this HtmlHelper helper)
		{
			var model = (Dexter.Web.Mvc.ViewModel.ViewsData.PublicBaseViewData)helper.ViewData.Model;

			return new MvcHtmlString(string.Format("<meta name=\"description\" content=\"{0}\" />", model.Description));
		}

		public static MvcHtmlString MetaKeywords(this HtmlHelper helper)
		{
			var model = (Dexter.Web.Mvc.ViewModel.ViewsData.PublicBaseViewData)helper.ViewData.Model;

			return new MvcHtmlString(string.Format("<meta name=\"keywords\" content=\"{0}\" />", model.KeyWords));
		}

		public static MvcHtmlString MetaAuthor(this HtmlHelper helper)
		{
			var model = (Dexter.Web.Mvc.ViewModel.ViewsData.PublicBaseViewData)helper.ViewData.Model;

			return new MvcHtmlString(string.Format("<meta name=\"author\" content=\"{0}\" />", model.Author));
		}

		public static MvcHtmlString MetaGenerator(this HtmlHelper helper)
		{
			return new MvcHtmlString(string.Format("<meta name=\"generator\" content=\"{0}\" />", DexterInfo.Generator));
		}

		public static MvcHtmlString HeadSuperPack(this HtmlHelper helper)
		{
			StringBuilder sb = new StringBuilder();
			sb.AppendLine(helper.MetaDescription().ToString());
			sb.AppendLine(helper.MetaKeywords().ToString());
			sb.AppendLine(helper.MetaAuthor().ToString());
			sb.AppendLine(helper.MetaGenerator().ToString());
			sb.AppendLine(helper.Index().ToString());
			sb.AppendLine(helper.Favicon().ToString());
			sb.AppendLine(helper.WindowsLiveWriterManifest().ToString());
			sb.AppendLine(helper.MetaWeblogRsd().ToString());
			sb.AppendLine(helper.OpenSearch().ToString());
			sb.AppendLine(helper.FeedUrl().ToString());
			sb.AppendLine(helper.PingBack().ToString());

			return new MvcHtmlString(sb.ToString());
		}
	}
}